<?php
class ItemsController extends AppController {

	var $name = 'Items';
	var $helpers = array('Html', 'Form');

	function beforeFilter() {
		parent::beforeFilter();

		if($this->Admin!=1)
		{
			$this->Auth->deny('*');
			$this->Auth->allow('index','view');
		}
		else
		{
			$this->Auth->allow('*');
		}
	}

	function index() {
		$this->pageTitle = 'All items';
		$this->Item->recursive = 1;
		$this->set('items', $this->paginate());

		$lang = $this->Session->read('Config.language');
		$this->currencyLoad($lang);
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Item.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('item', $this->Item->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Item->create();
			if ($this->Item->save($this->data)) {
				$this->Session->setFlash(__('The Item has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Item could not be saved. Please, try again.', true));
			}
		}
		$tags = $this->Item->Tag->find('list');
		$categories = $this->Item->Category->find('list');
		$this->set(compact('tags', 'categories'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Item', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Item->save($this->data)) {
				$this->Session->setFlash(__('The Item has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Item could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Item->read(null, $id);
		}
		$tags = $this->Item->Tag->find('list');
		$categories = $this->Item->Category->find('list');
		$this->set(compact('tags','categories'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Item', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Item->del($id)) {
			$this->Session->setFlash(__('Item deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}


}
?>